//
//  CircleCollectionViewCell.m
//  Beauty
//
//  Created by Snow on 16/9/6.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "CircleCollectionViewCell.h"
#import "ShopingBagCollectionViewCell.h"
#import "GXMacro.h"
#import "GXNetWorkingParser.h"
#import "ModelOfShopping.h"
#import "HeadCollectionReusableView.h"
#import "ModelOfGoodsInShopping.h"

@interface CircleCollectionViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) ShopingBagCollectionViewCell *cell;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) UICollectionReusableView *headerView;
@property (nonatomic, strong) NSArray *arrayOfGood;
@property (nonatomic, strong) NSMutableArray *arrayOfStore;
@property (nonatomic, strong) NSDictionary *dictionary;
@end
@implementation CircleCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self handleData];
        _layout = [[UICollectionViewFlowLayout alloc] init];
        _layout.minimumLineSpacing = 0;
        _layout.minimumInteritemSpacing = 0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_collectionView];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[ShopingBagCollectionViewCell class] forCellWithReuseIdentifier:@"ShopingBagCollectionViewCell"];
        [_collectionView registerClass:[HeadCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeadCollectionReusableView"];
        [self createFresh];
    }
    return self;
}
- (void)createFresh
{
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
    }];
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(handleLoadNewData)];
}
- (void)handleLoadNewData {
    //请求服务器更新数据
    NSUserDefaults *userdefault = [NSUserDefaults standardUserDefaults];
    NSString *password = [userdefault objectForKey:@"password"];
    _arrayOfStore = [NSMutableArray array];
    if (password.length == 0) {
        if (self.delegate != nil && [self.delegate respondsToSelector:@selector(backToVC)]) {
            [self.delegate backToVC];
        }
    } else {
        NSString *url = [NSString stringWithFormat:@"http://www.wantease.com/mobile1/index.php?act=member_cart&op=cart_list&key=%@", password];
        [GXNetWorkingParser GETWithURL:url par:nil success:^(id responseObject) {
            _dictionary = [NSDictionary dictionaryWithDictionary:responseObject[@"datas"]];
            NSLog(@"%@", responseObject);
            NSArray *arr = [_dictionary objectForKey:@"cart_list"];
            for (NSDictionary *dict in arr) {
                ModelOfShopping *model = [[ModelOfShopping alloc]init];
                [model setValuesForKeysWithDictionary:dict];
                [_arrayOfStore addObject:model];
            }
            [_collectionView reloadData];
        } filed:^(NSError *error) {
            
        }];
    }    [self.collectionView.mj_header endRefreshing];
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    _layout.itemSize = CGSizeMake(self.contentView.bounds.size.width, 150);
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView.mas_top);
        make.left.mas_equalTo(self.contentView.mas_left);
        make.right.mas_equalTo(self.contentView.mas_right);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).mas_equalTo(- 49 - 64 - 50);
    }];
}
- (void)handleData
{
    NSUserDefaults *userdefault = [NSUserDefaults standardUserDefaults];
    NSString *password = [userdefault objectForKey:@"password"];
    _arrayOfStore = [NSMutableArray array];
    if (password.length == 0) {
        if (self.delegate != nil && [self.delegate respondsToSelector:@selector(backToVC)]) {
            [self.delegate backToVC];
        }
    } else {
        NSString *url = [NSString stringWithFormat:@"http://www.wantease.com/mobile1/index.php?act=member_cart&op=cart_list&key=%@", password];
        [GXNetWorkingParser GETWithURL:url par:nil success:^(id responseObject) {
            _dictionary = [NSDictionary dictionaryWithDictionary:responseObject[@"datas"]];
            NSLog(@"%@", responseObject);
            NSArray *arr = [_dictionary objectForKey:@"cart_list"];
            for (NSDictionary *dict in arr) {
                ModelOfShopping *model = [[ModelOfShopping alloc]init];
                [model setValuesForKeysWithDictionary:dict];
                [_arrayOfStore addObject:model];
            }
            [_collectionView reloadData];
        } filed:^(NSError *error) {
            
        }];
    }
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return _arrayOfStore.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    ModelOfShopping *model = [_arrayOfStore objectAtIndex:section];
    NSArray *arr = model.goods;
    return arr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ShopingBagCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ShopingBagCollectionViewCell" forIndexPath:indexPath];
    ModelOfShopping *model = [_arrayOfStore objectAtIndex:indexPath.section];
    NSMutableArray *array = [NSMutableArray array];
    for (NSDictionary *dic in model.goods) {
        ModelOfGoodsInShopping *model = [[ModelOfGoodsInShopping alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [array addObject:model];
    }
    ModelOfGoodsInShopping *modelOfCell = [array objectAtIndex:indexPath.row];
    [cell.buttonOfBuy setImage:[UIImage imageNamed:@"ButtonOfSelected"] forState:UIControlStateNormal];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:modelOfCell.goods_image_url]];
    cell.labelOfHuge.text = modelOfCell.goods_spec;
    cell.labelOfPrice.text =[NSString stringWithFormat:@"¥ %@", modelOfCell.goods_price];
    cell.labelOfCount.text = [NSString stringWithFormat:@"x %@", modelOfCell.goods_num];
    cell.labelOfHuge.textColor = [UIColor lightGrayColor];
    cell.labelOfDetail.text = modelOfCell.goods_name;
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        HeadCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeadCollectionReusableView" forIndexPath:indexPath];
        ModelOfShopping *model = [_arrayOfStore objectAtIndex:indexPath.section];
        view.backgroundColor = [UIColor whiteColor];
        view.label.text = model.store_name;
        [view.buttonOfBuy setImage:[UIImage imageNamed:@"ButtonOfselect"] forState:UIControlStateNormal];
        return view;
    } else {
        return nil;
    }
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(SCREEN_WIDTH, 100);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(SCREEN_WIDTH, 40);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ModelOfShopping *model = [_arrayOfStore objectAtIndex:indexPath.section];
    NSMutableArray *array = [NSMutableArray array];
    for (NSDictionary *dic in model.goods) {
        ModelOfGoodsInShopping *model = [[ModelOfGoodsInShopping alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [array addObject:model];
    }
    ModelOfGoodsInShopping *modelOfCell = [array objectAtIndex:indexPath.row];
    if ([self.delegate respondsToSelector:@selector(shopingWithGood_id:)] && self.delegate != nil) {
        [self.delegate shopingWithGood_id:modelOfCell.goods_id];
    }
}
@end
